﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 物品管理
/// </summary>
public class ArticleManager : MonoBehaviour {

    #region 单例模式
    private static ArticleManager _instance;
    public static ArticleManager Instance {
        get {
            if (_instance == null) {
                _instance = GameObject.Find("ArticleManager").GetComponent<ArticleManager>();
            }
            return _instance;
        }
    }
    #endregion

    private Canvas Canvas;
    private Knapsack Knapsack;
    private PropPanel PropPanel;
    private WuxuePanel WuxuePanel;
    private ToolTip ToolTip;
    public PickUpArticle PickUpArticle { get; private set; }
    public bool IsPickUp {
        get {
            if (this.PickUpArticle == null) {
                return false;
            }
            return this.PickUpArticle.IsPickUp;
        }
    }

    void Start() {
        this.Canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
        this.Knapsack = this.Canvas.transform.Find("Knapsack").GetComponent<Knapsack>();
        this.PropPanel = this.Canvas.transform.Find("PropPanel").GetComponent<PropPanel>();
        this.WuxuePanel = this.Canvas.transform.Find("WuxuePanel").GetComponent<WuxuePanel>();
        this.ToolTip = this.Canvas.transform.Find("ToolTip").GetComponent<ToolTip>();
        this.PickUpArticle = this.Canvas.transform.Find("PickUpArticle").GetComponent<PickUpArticle>();
    }

    void Update() {
        this.ToolTip.FollowMouse(this.Canvas);
        this.PickUpArticle.FollowMouse(this.Canvas);
    }

    /// <summary>
    /// 根据物品模板id查询物品模板
    /// </summary>
    /// <param name="articleTempletId">物品模板id</param>
    /// <returns></returns>
    public ArticleTemplet GetArticleTemplet(string articleTempletId) {
        return GlobalData.Instance.ArticleTempletDictionary[articleTempletId];
    }

    public void StoreArticle() {
        ArticleTemplet articleTemplet = this.GetArticleTemplet("85651694a187480b8375e823384355f9");
        Article article = articleTemplet.GenerateArticle();
        this.StoreArticle(article, 1);

        articleTemplet = this.GetArticleTemplet("3a8022bd897e4f56b949cef71edb22f7");
        article = articleTemplet.GenerateArticle();
        this.StoreArticle(article, 1);

        articleTemplet = this.GetArticleTemplet("df3404d89a90451cbf51b92717248e70");
        article = articleTemplet.GenerateArticle();
        this.StoreArticle(article, 1);

        articleTemplet = this.GetArticleTemplet("a395d0c74ac24ef8b0354b16bde0d3ba");
        article = articleTemplet.GenerateArticle();
        this.StoreArticle(article, 4);
    }

    /// <summary>
    /// 存储物品
    /// 如果容量是1，则找一个空格子存储；
    /// 如果容量不是1，则顺序查找一个id相同且未存满的格子 或者 一个空格子
    /// </summary>
    /// <param name="article"></param>
    /// <param name="amount"></param>
    public void StoreArticle(Article article, int amount = 1) {
        ArticleSlot articleSlot = this.Knapsack.GetNotFullSlot(article);
        int overflow = articleSlot.StoreArticle(article, amount);
        if (overflow > 0) { // 如果存不下，则递归添加物品
            this.StoreArticle(article, overflow);
        }
    }

    /// <summary>
    /// 显示/隐藏背包
    /// </summary>
    public void ToggleKnapsack() {
        this.Knapsack.Toggle();
    }

    /// <summary>
    /// 显示/隐藏状态面板
    /// </summary>
    public void TogglePropPanel() {
        this.PropPanel.Toggle();
    }

    /// <summary>
    /// 显示/隐藏武学面板
    /// </summary>
    public void ToggleWuxuePanel() {
        this.WuxuePanel.Toggle();
    }

    /// <summary>
    /// 显示物品提示
    /// </summary>
    /// <param name="title"></param>
    /// <param name="content"></param>
    /// <param name="position"></param>
    public void ShowToolTip(string title, string content, Vector2 position) {
        this.ToolTip.Show(title, content, position);
    }

    public void ShowToolTip(ArticleSlot articleSlot, Vector2 position) {
        this.ToolTip.Show(articleSlot, position);
    }

    /// <summary>
    /// 隐藏物品提示
    /// </summary>
    public void HideToolTip() {
        this.ToolTip.Hide();
    }

    public void RefreshToolTip(string title, string content) {
        this.ToolTip.Refresh(title, content);
    }

    public void RefreshToolTip() {
        this.ToolTip.Refresh();
    }

    /// <summary>
    /// 捡起物品
    /// </summary>
    /// <param name="article">物品信息</param>
    /// <param name="amount">数量</param>
    public void PickUp(ArticleItem articleItem, int amount) {
        this.PickUpArticle.PickUp(articleItem, amount);
    }

    /// <summary>
    /// 拿起所有物品
    /// </summary>
    /// <param name="articleSlot"></param>
    public void PickUpALL(ArticleSlot articleSlot) {
        if (articleSlot.IsNotEmpty()) {
            int amount = articleSlot.PickUpALL();
            this.PickUpArticle.PickUp(articleSlot.ArticleItem, amount);
        }
    }

    /// <summary>
    /// 拿起一半物品
    /// </summary>
    /// <param name="articleSlot"></param>
    public void PickUpHalf(ArticleSlot articleSlot) {
        if (articleSlot.IsNotEmpty()) {
            int amount = articleSlot.PickUpHalf();
            this.PickUpArticle.PickUp(articleSlot.ArticleItem, amount);
        }
    }

    /// <summary>
    /// 放下物品
    /// 判断是否是装备
    ///     如果是装备，则继续判断是否可以放置该类型的装备
    ///         如果可以放置，则进行放置
    ///         如果不能放置，则简单提示
    ///     如果不是装备，则进行放置
    /// </summary>
    /// <param name="articleSlot"></param>
    public void PutDown(ArticleSlot articleSlot) {
        Constant.ArticleCategory articleCategory = articleSlot.ArticleCategory;
        if (articleCategory != Constant.ArticleCategory.Anything) { // 如果是装备
            Article article = this.PickUpArticle.ArticleInfo.Article;
            if (articleCategory != article.ArticleTemplet.ArticleCategory) { // 不是该类型装备
                print("物品不是" + ObjectHelper.GetDescription(articleCategory) + "，无法装备");
            } else { // 是该类型装备，则判断是否能够装备
                Personage personage = GlobalData.Instance.GetPlayerPersonage();
                if (article.AbleEquip(personage).Count == 0) { // 满足条件
                    articleSlot.PutDown(this.PickUpArticle);
                } else {
                    print("不满足装备条件，无法装备");
                }
            }   
        } else {
            articleSlot.PutDown(this.PickUpArticle);
        }
    }

    /// <summary>
    /// 脱下装备
    /// </summary>
    /// <param name="equipmentSlot"></param>
    public void PutOffEquipment(EquipmentSlot equipmentSlot) {
        if (equipmentSlot.IsNotEmpty()) { // 如果有装备，则脱下装备
            Article article = equipmentSlot.ArticleInfo.Article;
            KnapsackSlot knapsackSlot = this.Knapsack.GetNotFullSlot(article);
            this.PickUpALL(equipmentSlot);
            this.PutDown(knapsackSlot);
        }
    }

    /// <summary>
    /// 丢弃物品
    /// 如果之前是全部拿出来，则处理格子
    /// 最后清空拿起物品
    /// </summary>
    public void DiscardArticle() {
        ArticleItem articleItem = this.PickUpArticle.ArticleItem;
        if (articleItem.IsPickUpAll) { // 如果是拿起的全部物品
            KnapsackSlot articleSlot = (KnapsackSlot) articleItem.GetArticleSlot();
            this.Knapsack.DiscardArticle(articleSlot);
        }
        this.PickUpArticle.PutDownAll();
    }

    /// <summary>
    /// 使用物品拿起的物品
    /// 如果是其他物品，不允许
    /// 如果是消耗品
    ///     使用效果
    ///     判断
    ///         如果是拿起所有物品
    ///             如果减少的数量与拿起的数量相同，则清空物品槽
    ///             如果减少的数量与拿起的数量不相同，则物品槽减少物品数量
    ///         如果是拿起部分物品，则不作其他处理
    ///     拿起的物品减少一件
    /// 如果是装备
    ///     往对应格子放装备
    /// </summary>
    public void UseArticle() {
        Article article = this.PickUpArticle.ArticleInfo.Article;
        Constant.ArticleCategory articleCategory = article.ArticleTemplet.ArticleCategory;
        if (articleCategory == Constant.ArticleCategory.Anything // 任何物品
            || articleCategory == Constant.ArticleCategory.QuestItem) { // 任务物品
            print("无法使用该物品");
            return;
        }
        
        if (articleCategory == Constant.ArticleCategory.Drug // 药品
            || articleCategory == Constant.ArticleCategory.Nutriment // 营养品
            || articleCategory == Constant.ArticleCategory.WuxueEsoterica) { // 武学秘籍
            /**
             * 物品效果
             */
            Personage personage = GlobalData.Instance.GetPlayerPersonage();
            personage.PutOnEquipment(article);
            this.PropPanel.UpdateProp();

            /**
             * 更新ui
             */
            ArticleItem articleItem = this.PickUpArticle.ArticleItem;
            if (articleItem.IsPickUpAll) { // 如果是拿起的全部物品
                KnapsackSlot articleSlot = (KnapsackSlot) articleItem.GetArticleSlot();
                ArticleInfo articleInfo = articleSlot.ArticleInfo;
                if (articleInfo.Amount == 1) { // 物品槽只有一件物品
                    this.Knapsack.DiscardArticle(articleSlot);
                } else {
                    articleSlot.AddAmount(articleItem, -1);
                }
            } else { // 拿起部分物品
                // 不处理
            }
            this.PickUpArticle.PutDown(); // 放下一件物品
        } else { // 装备
            EquipmentSlot articleSlot = this.PropPanel.GetEquipmentSlot(articleCategory);
            this.PutDown(articleSlot);
        }
    }

    /// <summary>
    /// 直接使用物品
    /// </summary>
    /// <param name="articleItem"></param>
    public void UseArticle(ArticleItem articleItem) {
        Article article = articleItem.ArticleInfo.Article;
        Constant.ArticleCategory articleCategory = article.ArticleTemplet.ArticleCategory;
        if (articleCategory == Constant.ArticleCategory.Anything // 任何物品
            || articleCategory == Constant.ArticleCategory.QuestItem) { // 任务物品
            print("无法使用该物品");
            return;
        }

        if (articleCategory == Constant.ArticleCategory.Drug // 药品
            || articleCategory == Constant.ArticleCategory.Nutriment // 营养品
            || articleCategory == Constant.ArticleCategory.WuxueEsoterica) { // 武学秘籍
            /**
             * 物品效果
             */
            Personage personage = GlobalData.Instance.GetPlayerPersonage();
            personage.PutOnEquipment(article);
            this.PropPanel.UpdateProp();

            /**
             * 更新ui
             */
            KnapsackSlot articleSlot = (KnapsackSlot) articleItem.GetArticleSlot();
            ArticleInfo articleInfo = articleSlot.ArticleInfo;
            if (this.Knapsack.ContainsKnapsackSlot(articleSlot)) { // 背包里使用物品
                if (articleInfo.Amount == 1) { // 物品槽只有一件物品
                    this.Knapsack.DiscardArticle(articleSlot);
                } else {
                    articleSlot.AddAmount(articleItem, -1);
                }
            } else { // 其他地方（如战斗时）使用物品
                if (articleInfo.Amount == 1) { // 物品槽只有一件物品
                    articleSlot.ClearArticle();
                } else {
                    articleSlot.AddAmount(articleItem, -1);
                }
            }
        } else { // 装备
            EquipmentSlot articleSlot = this.PropPanel.GetEquipmentSlot(articleCategory);
            this.PickUpALL(articleItem.GetArticleSlot()); // 模拟拿起
            this.PutDown(articleSlot);
        }
    }

    /// <summary>
    /// 更新人物属性
    /// </summary>
    /// <param name="oldArticle"></param>
    /// <param name="newArticle"></param>
    public void UpdatePersonageProp(Article oldArticle, Article newArticle) {
        Personage personage = GlobalData.Instance.GetPlayerPersonage();
        if (oldArticle != null) {
            personage.PutOffEquipment(oldArticle);
        }
        if (newArticle != null) {
            personage.PutOnEquipment(newArticle);
        }
        this.PropPanel.UpdateProp();
    }
}
